<?php
App::uses('Sanitize','Utility');
class ProductsController extends AppController {
	var $uses = array('Product','Size','Stock');
	public function beforeFilter() {
        parent::beforeFilter();
		$this->Auth->allow();
    }
    
	public function view($brand = 'all', $category_slug = null, $type = 'new'){
		$conditions = array();
		if($brand != 'all'){
			$brand = Sanitize::escape($brand);
			$conditions['and']['Brand.name'] = $brand;
		}
		if($category_slug != null){
			$category_slug = Sanitize::escape($category_slug);
			$conditions['and']['Category.slug'] = $category_slug;
		}
		if($type == 'new'){
			$order = 'Product.created desc,Product.product_code desc';
		}
		else {
			$order = 'Product.id asc';
		}
		$this->paginate = array(
			'limit'=>12,
			'conditions'=>$conditions,
			'order'=>$order,
			'recursive'=>0,
		);
		$rs = $this->paginate('Product');
		$this->set(compact('rs','brand'));
	}
	
	public function search(){
		$conditions = array();
		$q = $this->params->query;
		$field = $category_id = $brand_id = $source = $price_from = $price_to = '';
		if(!empty($q['field'])){
			$field = Sanitize::escape($q['field']);
			$conditions['or']['Product.name like '] = "%$field%";
			$conditions['or']['Product.description like '] = "%$field%";
		}
		
		if(!empty($q['category_id'])){
			$category_id = Sanitize::escape($q['category_id']);
			$conditions['and']['Product.category_id'] = $category_id;
		}
		
		if(!empty($q['brand_id'])){
			$brand_id = Sanitize::escape($q['brand_id']);
			$conditions['and']['Product.brand_id'] = $brand_id;
		}
		
		if(!empty($q['source'])){
			$source = Sanitize::escape($q['source']);
			$conditions['and']['Product.source'] = $source;
		}
		
		if(!empty($q['price_from'])){
			$price_from = Sanitize::escape($q['price_from']);
			$conditions['and']['Product.price >='] = $price_from;
		}
		
		if(!empty($q['price_to'])){
			$price_to = Sanitize::escape($q['price_to']);
			$conditions['and']['Product.price <='] = $price_to;
		}
		
		$this->paginate = array(
			'limit'=>12,
			'conditions'=>$conditions,			
			'recursive'=>0,
		);
		$rs = $this->paginate('Product');
		
		$sources = $this->Product->find('all',array(
			'joins'=>array(
				array(
				'table'=>'stocks',
				'alias'=>'Stock',
				'conditions'=>array('Stock.product_id = Product.id'),
				'type'=>'inner',
				),
			),
			'fields'=>array('DISTINCT Product.source'),
			'conditions'=>array('del_flag'=>0))
		);
		$this->set(compact('rs','field','category_id','brand_id','sources','source','price_from','price_to'));
		if($this->request->is('ajax')){
			$this->layout = 'ajax';
			$this->render('ajax_search');
		}
	}
	
	public function detail($slug = null){
		if($slug != null){
			$slug = Sanitize::escape($slug);
			$this->Product->recursive = 0;
			$rs = $this->Product->find('first',array(
				'conditions' => array('Product.slug' => $slug),
			));
			$same_cat = $stocks = array();
			if($rs){
				$this->Product->id = $rs['Product']['id'];
				$this->Product->saveField('views',$rs['Product']['views']+1);
				$stocks = $this->Stock->find('all',array(
					'fields' => array('Stock.amount','Size.id','Size.name'),
					'conditions'=>array(
						'product_id'=>$rs['Product']['id'],
					),
					'joins' => array(
						array(
							'table' => 'sizes',
							'alias' => 'Size',
							'conditions' => array('Stock.size_id = Size.id'),
							'type' => 'inner',
						),
					),
				));
				$same_cat = $this->Product->find('all',array(
					'conditions' => array(
						'Product.slug <>' => $slug,
						'category_id' => $rs['Product']['category_id'],
						'brand_id' => $rs['Product']['brand_id'],
					),
					'limit' => 8,
				));
			}
			
			$opt_size = $this->Size->find('list',array('fields'=>array('id','name')));
			$this->set(compact('rs','opt_size','same_cat','stocks'));
		}		
	}
}
